package com.web;

import com.alibaba.fastjson.JSONArray;
import com.bean.Subsidy;
import com.service.ISubsidyManagementService;
import com.service.SubsidyManagementServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/SubsidySearchServlet")
public class SubsidySearchServlet extends HttpServlet
{
    private ISubsidyManagementService subsidyManagementService = new SubsidyManagementServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        //获取参数列表
        String subsidyProjectName = req.getParameter("subsidy_project_name" );

        //开始根据名字查找项目
        System.out.println("查询了项目名称： " + subsidyProjectName);
        Subsidy[] subsidies = subsidyManagementService.SearchSubsidyByName(subsidyProjectName);

        //将数据转换成JSON并写入页面通过ajax返回
       String subsidyListJSON = JSONArray.toJSONString(subsidies);
        System.out.println(subsidyListJSON);
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(subsidyListJSON);
    }
}
